$.ajaxSetup({
  cache: false
});


function getData()
{
  $.getJSON("Mkart/key.php", {}, function(response){
                 for (var x = 0; x < response.length; x++) {
					 if([response[x].name] == "kart"){
						var _kart_reg = new RegExp('\\[' +'kart'+'\\]', 'g');
				        var _kart_match = $(document.body).text().match(_kart_reg);
				         if(_kart_match){
					    var kart_word = "<div id='kart_table'></div>" ;
						replaceword('['+response[x].name+']',kart_word) ;

						 }
					 }else{
						 callbody([response[x].name]) ;
					 }
				 }
				 var _list_reg = new RegExp('\\[' +'kart_list'+'\\]', 'g');
				var _list_match = $(document.body).text().match(_list_reg);
				if(_list_match){
				 var list_word = "<div id='kart_list'></div>" ;
				replaceword('[kart_list]',list_word) ;
				
				chkkart_list() ;
				}
				chkkart() ;
  });
}


function callbody(_key){
var _results = [];
var _reg = new RegExp('\\[' + _key + '=(\\w+)\\]', 'g');
var _matched = $(document.body).text().match(_reg);

if(_matched){//判斷key值是否存在頁面

for(var _cnt = 0 ; _cnt < _matched.length ; _cnt++){
    _results.push(
        /\[\w+=(\w+)\]/.exec(_matched[_cnt])[1]
    );
}

for(var m = 0 ; m < _results.length ; m++){
  $.getJSON("Mkart/data.php?key="+_key+"&id="+_results[m], {}, function(data){
	  var chang_word = "" ;
	  var _keyword = new RegExp('\\[' +data.word +'\\]', 'g');
	  if(data.price){
	  chang_word = '$'+data.price ;
	  }else if(data.buttonword){
	  chang_word = data.buttonword ;
	  }
	  replaceword(_keyword,chang_word);	  
  });
}
}//end check
}

function chkkart(_key){
//var _kart_reg = new RegExp('\\[' +'kart'+'\\]', 'g');
var _kart_match = document.getElementById('kart_table');
	if(_key){
  $.get("Mkart/kart_data.php?pid="+_key, function(ta){
	  var chang_word = "" ;	  
	  chang_word = ta ;
	  if(chang_word && _kart_match){
	  document.getElementById('kart_table').innerHTML= chang_word ;
	  }
  });
	}else{
	  $.get("Mkart/kart_data.php", function(ta){
	  var chang_word = "" ;	  
	  chang_word = ta ;
	  if(chang_word && _kart_match){
	  document.getElementById('kart_table').innerHTML= chang_word ;
	  }
  });
	}
}

function chkkart_list(){
	  $.get("Mkart/kart_list.php", function(ta){
	  var chang_word = "" ;	  
	  chang_word = ta ;
	  if(chang_word){
	  document.getElementById('kart_list').innerHTML= chang_word ;
	  }
  });
}

function change_count(_id,_num,_count){
	  $.get("Mkart/kart_list.php?pid="+_id+"&count="+_count+"&num="+_num, function(ta){
	  var chang_word = "" ;	  
	  chang_word = ta ;
	  if(chang_word){
	  document.getElementById('kart_list').innerHTML= chang_word ;
	  }
	  chkkart();
  });  
}

function cancel_prod(_num){
$.get("Mkart/kart_cancel.php?num="+_num,function(){
//alert(test);
chkkart_list();
chkkart();
});

}

function replaceword(a,b) {
var text = "";
text = document.body.innerHTML;
text = text.replace(a, b);
document.body.innerHTML = text;
}


$(document).ready(function(){getData()});